//
//  UIView+YTAdd.m
//  Elife
//
//  Created by 拓客营 on 2022/3/30.
//

#import "UIView+YTAdd.h"

@implementation UIView (YTAdd)

-(void)themeGradientCornerRadius:(float)cornerRadius Size:(CGSize)size {
    [self layerFromColor:kRGB(0x6AD9AF) toColor:kRGB(0x31ADAB) cornerRadius:cornerRadius size:size];
}

-(void)themeGradientSize:(CGSize)size {
    [self customGradientFrom:kRGB(0x6AD9AF) toColor:kRGB(0x31ADAB) size:size];
}

- (void)layerFromColor:(UIColor *)leftColor toColor:(UIColor *)rightColor cornerRadius:(float)cornerRadius size:(CGSize)size {
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 0);
    gl.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    gl.cornerRadius = cornerRadius;
    
    [self.layer insertSublayer:gl atIndex:0];
}

-(void)customGradientFrom:(UIColor *)leftColor toColor:(UIColor *)rightColor size:(CGSize)size {
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 0);
    gl.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    [self.layer insertSublayer:gl atIndex:0];
}

-(void)customGradientFromTop:(UIColor *)topColor toBottomColor:(UIColor *)bottomColor size:(CGSize)size {
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, size.width, size.height);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(0, 1);
    gl.colors = @[(__bridge id)topColor.CGColor,(__bridge id)bottomColor.CGColor];
    gl.locations = @[@(0),@(1.0f)];
    
    [self.layer insertSublayer:gl atIndex:0];
}

@end
